<?php 

namespace App\Models\ModelFilters;

use EloquentFilter\ModelFilter;

class MachineFilter extends ModelFilter
{
    /**
    * Related Models that have ModelFilters as well as the method on the ModelFilter
    * As [relationMethod => [input_key1, input_key2]].
    *
    * @var array
    */
    public $relations = [];

    public function imei($imei)
    {
    	return $this->whereLike('imei', $imei);
    }

    public function id($id)
    {
        return $this->where('id',$id);
    }

    public function broker($broker)
    {
    	return $this->whereHas('machine_managers', function($query) use($broker){
    		$query->where('manager_id', $broker);
    	});
    }

    public function merchant($merchant)
    {
    	return $this->whereHas('machine_managers', function($query) use($merchant){
    		$query->where('manager_id', $merchant);
    	});
    }

    public function operator($operator)
    {
        return $this->whereHas('machine_managers', function($query) use($operator){
            $query->where('manager_id', $operator);
        });
    }

    public function mainborad($mainborad){
        return $this->whereHas('mainborad', function($query) use ($mainborad){
            $query->where('id',$mainborad);
        });
    }

    public function online($online)
    {
    	switch ($online) {
    		case 0:
    			return $this->where('connected_at', null)->where('disconnected_at',null);
    			break;
    		case 1:
    			return $this->where('connected_at', '<>', null);
    			break;
    		case 2:
    			return $this->where('disconnected_at', '<>', null);
    			break;
    	}
    }

    public function expireTime($expire_time)
    {
    	$expire_time = explode(',', $expire_time);
    	return $this->whereBetween('expire_time', $expire_time);
    }
}
